<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 路线管理控制器-后台
 * 
 * @desc    路线管理
 * 
 * @desc    完善文档注释
 * @version 1.1.0
 * @date	2017年6月27日 下午6:01:33
 * 
 * @version	1.2.0
 * @desc	加入conditions<span style="color:red;font-weight:bold;">正式运行</span>！
 * @date	2017年7月17日 下午9:06:04
 * 
 * @title 路线管理
 * @entry http://discovery.segway.com
 */
class Touroute extends MY_Manager_Controller
{
	/** @var Lib_touroute */
	public $lib_touroute = null;
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->library('lib_touroute');
		
		$this->set_mutable_view_var('head.title', 'Tour - Management');
		$this->set_mutable_view_var('head.keywords', '');
		$this->set_mutable_view_var('head.description', '');
		
		$this->lang->load('touroute');
		
	}
	
	/**
	 * 首页 -> 列表页
	 *
	 * @return html
	 *
	 * @command pass noneurl
	 */
	public function index()
	{
		$this->listing();
	}
	
	/**
	 * 路线列表页
	 * 
	 * @desc    list -> tourlist -> itemlist -> table -> tabulate -> listing
	 * @method  GET
	 * @author	Jason
	 * @date	2017年5月23日 下午6:32:42
	 * @link	/manage/touroute/listing
	 * 
	 * @desc	加入conditions试行
	 * @date	2017年6月29日 下午18:05:34
	 * 
	 * @version	1.1.0
	 * @desc	加入conditions<span style="color:red;font-weight:bold;">正式运行</span>！
	 * @date	2017年7月17日 下午9:06:04
	 * @----
	 * @param	conditions	GET参数，POST也可。JSON格式，包含两个键：查询条件和排序条件。
	 * eg:
	{
		"where_and": [
			{"operator":"like","field":"name","value":"segwa","modifier":"raw"}, 
			{"operator":"bitwise","field":"status","value":["enabled","checked"],"modifier":"status_tags"}, 
			{"operator":"in","field":"member_id","value":"merchant","modifier":"foreign_member_name"}, 
			{"operator":"in","field":"merchant_id","value":[106,108],"modifier":"raw"}, 
			{"operator":"eq","field":"country","value":"693000","modifier":"raw"}, 
			{"operator":"eq","field":"city","value":"86690000","modifier":"raw"}, 
			{"operator":"gt","field":"create_time","value":"2016-12-31 23:59:59","modifier":"time_string"}, 
			{"operator":"lt","field":"create_time","value":"2017-06-29 12:23:34","modifier":"time_string"}, 
			{"operator":"ge","field":"retail_cny","value":"1000","modifier":"raw"}, 
			{"operator":"le","field":"retail_cny","value":"1999","modifier":"raw"}
		], 
		"order_by": [
			{"field":"duration","direction":"DESC"}, 
			{"field":"mileage","direction":"DESC"}, 
			{"field":"page_view","direction":"DESC"}, 
			{"field":"retail_cny","direction":"ASC"}
		]
	}
	 * 
	 */
	public function listing($page = 1, $number = 20)
	{
		$cookie_lang = $this->lib_cookie->get_language();
		$language = $this->lib_language->get_supported_language($this->language, $cookie_lang);
		
		$conditions = $this->lib_touroute->get_conditions();
		$list = $this->lib_touroute->get_touroutes_for_manage_list($conditions, $language, $page, $number);
		$data['list'] = $list;
		
		$this->load->view('manager/touroute/list', $data);
	}
	// MARK ajax 列表页
	/**
	 * 路线列表页，ajax，JSON
	 *
	 * @desc    list -> tourlist -> itemlist -> table -> tabulate -> listing
	 * @method  GET
	 * @author	Jason
	 * @date	2017年5月23日 下午6:32:42
	 * @link	/manage/touroute/ajax_listing
	 */
	public function ajax_listing($page = 1, $number = 20)
	{
		$cookie_lang = $this->lib_cookie->get_language();
		$language = $this->lib_language->get_supported_language($this->language, $cookie_lang);
		
		$conditions = $this->lib_touroute->get_conditions();
		$list = $this->lib_touroute->get_touroutes_for_manage_list($conditions, $language, $page, $number);
		$data['list'] = $list;
		
		Lib_json::renderSuccess($data);
	}
	
	/**
	 * 路线上下架
	 *
	 * @method  POST
	 * @author  Jason
	 * @date    2017年5月24日 上午10:26:52
	 * 
	 * @param   number $id
	 * 
	 * @return  JSON	标准JSON格式响应
	 * 
	 * @link    /manager/touroute/ajax_toggle_shelve
	 * 
	 * @command noneurl
	 */
	public function ajax_toggle_shelve()
	{
		$id = $this->input->post_get('id');
		list($status, $content) = $this->lib_touroute->toggle_status_enabled($id);
		
		if(!$status) {
			Lib_json::renderError(Lib_json::ERROR_BUSINESS, current($content));
		} else {
			Lib_json::renderSuccess(['data' => $content]);
		}
	}
	
	/**
	 * 路线审核通过与不通过
	 *
	 * @method  POST
	 * @author  Jason
	 * @date	2017年6月20日 下午3:15:26
	 *
	 * @param   number $id			ID
	 * @param   string $judgement	通过还是不通过，accepted/rejected，默认拒绝
	 * @param   string $comment		通过备注，不通过原因，可选
	 *
	 * @return  JSON	标准JSON格式响应
	 *
	 * @link    /manager/touroute/ajax_change_checking_status
	 * 
	 * @command noneurl
	 */
	public function ajax_change_checking_status()
	{
	
		#$_POST['module'] = 'touroute';
		#$this->lib_touroute->open_checking_switch_status($this->input->post('id'));
		list($status, $content) = $this->lib_touroute->change_checking_status();
	
		if(!$status) {
			Lib_json::renderError(Lib_json::ERROR_BUSINESS, current($content));
		} else {
			Lib_json::renderSuccess(['data' => $content]);
		}
	}
	
	/**
	 * 路线详情 - 编辑前信息载入
	 * 
	 * @desc    编辑前信息载入
	 * @method  GET
	 * @author  Jason
	 * @date	2017年5月24日 下午4:23:42
	 * 
	 * @param   number $id       ID，默认为 0，代表新增一条记录
	 * @param   string $language 默认为 ""，从 COOKIE 中取，取不到则默认为 <default>"english"
	 * 			把参数按顺序辍到 URL 之后，用 "/" 间隔
	 * @example 如果参数为 id=45 language=chinese
	 * 			那么URL为 /manage/touroute/detail/45/chinese
	 * 
	 * @demoParam id 45
	 * @demoParam language chinese
	 */
	public function detail($id = 0, $language = '')
	{
		$cookie_lang = $this->lib_cookie->get_language();
		$language = $this->lib_language->get_supported_language($language, $cookie_lang);
		$currency = $this->lib_cookie->get_currency();
		
		$touroute = $this->lib_touroute->get_touroute_for_detail($id, $language, $currency);
		if (empty($touroute)) {
			$touroute = [];
		}
		
		$touroute_periods = $this->lib_touroute->get_touroute_periods_for_detail($id);
		$this->load->helper('array');
		
		$data['content']['touroute'] = $touroute;
		$data['content']['touroute_periods'] = $touroute_periods;
		
		$this->load->view('manager/touroute/detail', $data);
	}
	
	/**
	 * 路线语言详情 - 编辑语言前信息载入
	 *
	 * @desc    编辑语言前信息载入
	 * @method  GET
	 * @author  Jason
	 * @date	2017年5月25日 下午2:55:56
	 *
	 * @param   number $id       ID，必填项！
	 * @param   string $language 默认为 ""，从 COOKIE 中取，取不到则默认为 <default>"english"
	 * 			把参数按顺序辍到 URL 之后，用 "/" 间隔
	 * @example 如果参数为 id=45 language=chinese
	 * 			那么URL为 /manage/touroute/detail_language/45/chinese
	 * 
	 * @demoParam id 45
	 * @demoParam language chinese
	 */
	public function detail_language($id, $language = '')
	{
		$cookie_lang = $this->lib_cookie->get_language();
		$language = $this->lib_language->get_supported_language($language, $cookie_lang);
		
		// 获取语言
		$resource = $id;
		$module = [$this->mod_touroute, 'master'];
		
		$data = $this->lib_language->select_language($resource, $module, $language);
		$data['content']['id'] = $id;
		$data['content']['language'] = $language;
		$data['content']['values'] = $data['values'];
		
		$this->load->view('manager/touroute/detail_language', $data);
	}
	
	
}
